// pages/user-profile-activate/user-profile-activate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '天河区'],

  },
  petname(e) {
    this.setData({
      petname: e.detail.value
    })
  },
  phone(e) {
    this.setData({
      phone: e.detail.value,
      phonelength: e.detail.value.length
    })
  },
  bindAddressChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  radioChange(e) {
    // console.log("单选框的值：", e.detail.value)
    this.setData({
      gender: e.detail.value
    })

  },
  bindSignature: function (e) {
    // console.log(e.detail.value)
    this.setData({
      signature: e.detail.value
    })
  },
  addchooseImage() {
    wx.chooseImage({
      success: res => {
        // console.log(res)
        this.setData({
          chooseImage: res.tempFilePaths[0]
        })
      }
    })
  },

  add(e) {
    if (this.data.extend == '已激活') {
      console.log('无法重复激活账号！')
      wx.showModal({
        title: '',
        content: '无法重复激活账号！',
      })
    } else {
      if (this.data.phonelength < 11) {
        console.log("手机号码的长度不正确")
        wx.showModal({
          title: '',
          content: '手机号码的长度不正确',
        })
      } else if (this.data.chooseImage == null || this.data.gender == null || this.data.petname == null || this.data.phone == null || this.data.signature == null) {
        console.log('内容不可为空！')
        wx.showModal({
          title: '',
          content: '内容不可为空！',
        })
      } else {
        var that = this;
        wx.request({
          url: 'http://localhost/shopping/users_add.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            id: that.data.id,
            img: that.data.chooseImage,
            gender: that.data.gender,
            petname: that.data.petname,
            phone: that.data.phone,
            address: that.data.region,
            signature: that.data.signature
          },
          success: function (res) {
            console.log(res.data)
            console.log("个人资料添加成功！")
            that.User();
            wx.showToast({
              title: '激活账号成功',
              icon: 'success',
              duration: 2000
            })
            that.setData({})
          },
          fail: function (res) {
            console.log("fail");
          }
        })
      }
    }


  },
  User: function () {
    var name = this.data.name
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/users.php',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        that.setData({
          arraa: res.data.users,
          id: res.data.users[0].id,
          extend: res.data.users[0].extend,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var name = e.name

    this.setData({
      name: name,

    })
    this.User();

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})